package com.xuchang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuchang.domain.SysPrivilege;
import com.xuchang.service.SysPrivilegeService;
import com.xuchang.mapper.SysPrivilegeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author xuchang
 * @description 针对表【sys_privilege(权限配置)】的数据库操作Service实现
 * @createDate 2025-06-29 22:19:06
 */
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege>
        implements SysPrivilegeService {
    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;

    /**
     * 获取该菜单下面所有的权限
     *
     * @param menuId 菜单的ID
     * @param roleId roleId 代表当前的查询的角色的ID
     * @return
     */
    @Override
    public List<SysPrivilege> findPrivilegeByMenu(Long menuId, Long roleId) {

        LambdaQueryWrapper<SysPrivilege> wrapper = new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId);
        List<SysPrivilege> list = list(wrapper);
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        for (SysPrivilege sysPrivilege : list) {
            Set<Long> privilegeByRoleId = sysPrivilegeMapper.findPrivilegeByRoleId(roleId);
            if (privilegeByRoleId.contains(sysPrivilege.getId())) {
                sysPrivilege.setOwn(1);
            }
        }
        return list;
    }
}




